class ContController < ApplicationController
  require 'digest/sha1'
  # before_filter :authenticate, :except => [:index, :meetingroom]
  
  def index
	
  end

  def update_photo
	if params[:user]
		someuser = User.find_or_initialize_by_id(session[:user_id])
		someuser.update_attributes(params[:user])
		redirect_to "/account"
	end
  end
  
  def meetingroom
  end
  
  def logout
	user = User.find(session[:user_id])
	user.online = false
	user.save
	reset_session;
	redirect_to "/"
  end
  
  def account
  
  end
  
  def login
	if params[:user]
		s1 = User.new(params[:user])
		s2 = User.find_by_username(s1.username)
		if s2 and s2.password == s1.password
			session[:user_id]=s2.id
			s2.online = true
			s2.save
			redirect_to "/"
		else
			flash[:error] = "Bad username and/or password"
		end
	else
	   
	end
  end
  
  def infos
	if params[:information]
			someinfos = Information.find_or_initialize_by_id(session[:user_id])
			someinfos.update_attributes(params[:information])
		if someinfos.save
			flash[:notice] = "Informations updated"
		else
			flash[:notice] = "Error when submitted..."
		end
	end
  end
  
  def edit
	if params[:user]
		someuser = User.find_or_initialize_by_id(session[:user_id])
		someuser.update_attributes(params[:user])
		
		redirect_to "/account"
		flash[:notice] = "Profil modified"
	end
  end
  
  def profile 
	@profil = User.find(params[:id])
  end
  
  def password
	if params[:user]
		if User.find_by_email(params[:user][:email])
			@new = User.find_by_email(params[:user][:email])
			@new.update_attributes(:password => Digest::SHA1.hexdigest("some-random-string")[8..16])
			UserMailer.forgot_password(@new).deliver
			flash[:notice]="Password send"
		else
			flash[:notice] = "Bad e-mail"
		end
	end	
  end
  
  def newpass
	if params[:user]
			someuser = User.find_or_initialize_by_id(session[:user_id])
			someuser.update_attributes(:password => params[:user][:password])
			redirect_to "/account"
			flash[:notice] = "Password modified"
	end
  end
  
  
end
